package com.maisonliberte.crawller;

import com.maisonliberte.conf.Config;
import com.maisonliberte.enums.CrawlCategory;
import com.maisonliberte.enums.CrawlTarget;

public class CrawlerHandler {

	public void productCrawling() throws Exception{
	
		for(CrawlTarget target : Config.getInstance().parsingTargetList.keySet()){

			for(CrawlCategory category : Config.getInstance().parsingTargetList.get(target).keySet()){
				
				try{
					
					String url = Config.getInstance().parsingTargetList.get(target).get(category).get(0).getUrl();
					String encode = Config.getInstance().parsingTargetList.get(target).get(category).get(0).getEncode();
					String buyer = Config.getInstance().parsingTargetList.get(target).get(category).get(0).getBuyer();
					AbstractCrawler crawler = Config.getInstance().executeClassList.get(target);
					crawler.setTarget(target);
					crawler.setCategory(category);
					crawler.setCharset(encode);
					crawler.setUrl(url);
					crawler.setBuyer(buyer);
					long timestart = System.currentTimeMillis() / 1000L;
					System.out.println(String.format("target : %s, category : %s crawling start", target, category));
					crawler.process();
					long timeend = System.currentTimeMillis() / 1000L;
					System.out.println(String.format("target : %s, category : %s crawling finish", target, category));
					System.out.println(String.format("process time : %d", timeend - timestart));
					
				}catch(RuntimeException e){
					System.out.println(e);
				}catch(Exception e){
					System.out.println(e);
				}
				
				//Thread.sleep(3000);
				
			}
		}
		
	}
	
	public void brandCrawling() throws Exception{
		
		String url = "http://tblog.stylestore.allabout.co.jp/blogs";
		AbstractBrandCrawler crawler = new StyleStoreBrandCrawler();
		crawler.setUrl(url);
		crawler.setCharset("UTF-8");
		crawler.process();
		
	}

}
